import request from "./request";
import { encode } from '../utils/base64'
import { ElMessage } from 'element-plus'
import downLoadNotice from '../hooks/downLoadNotice'

//刷新token
export const NewAccess = () => {

    return request({
        url: `/v1/User/NewAccess`,
        method: "post",
    })
}
//登录
export const login = (data: { id: number, password: string }) => {
    let arr = encode(data)

    return request({
        url: 'v1/User/Login',
        method: "post",
        data: { "raw": arr }
    })
}



//注册
export const register = (data: any) => {
    let arr = encode(data)

    return request({
        url: '/v1/User/Create',
        method: "post",
        data: { "raw": arr }
    })
}


//提交问卷
export const invesStudentCreate = (data: any) => {
    let arr = encode(data)

    return request({
        url: '/v1/Inves/Student/Create',
        method: "post",
        data: { "raw": arr }
    })
}
export const invesTeacherCreate = (data: any) => {
    let arr = encode(data)

    return request({
        url: '/v1/Inves/Teacher/Create',
        method: "post",
        data: { "raw": arr }
    })
}



//筛选问卷
export const filterInvesS = (data: any) => {
    let arr = encode(data)
    if (data.district_id === undefined) {
        if (data.gender != undefined)
            return request({
                url: `/v1/Inves/Student/Filter?page=${data.page}&size=5&city_id=${data.city_id}&gender=${data.gender}`,
                method: "post",
                data: { "raw": arr }
            })
        else return request({
            url: `/v1/Inves/Student/Filter?page=${data.page}&size=5&city_id=${data.city_id}`,
            method: "post",
            data: { "raw": arr }
        })
    }

    else {
        if (data.gender != undefined) {
            if (data.city_id === 11) {
                return request({
                    url: `/v1/Inves/Student/Filter?page=${data.page}&size=5&city_id=${data.city_id}&district_id=${data.district_id}&gender=${data.gender}`,
                    method: "post",
                    data: { "raw": arr }
                })
            }
            else return request({
                url: `/v1/Inves/Student/Filter?page=${data.page}&size=5&district_id=${data.district_id}&gender=${data.gender}`,
                method: "post",
                data: { "raw": arr }
            })
        }

        else {
            if (data.city_id === 11) {
                return request({
                    url: `/v1/Inves/Student/Filter?page=${data.page}&size=5&city_id=${data.city_id}&district_id=${data.district_id}`,
                    method: "post",
                    data: { "raw": arr }
                })
            }
            else return request({
                url: `/v1/Inves/Student/Filter?page=${data.page}&size=5&district_id=${data.district_id}`,
                method: "post",
                data: { "raw": arr }
            })
        }
    }

}
export const filterInvesT = (data: any) => {
    let arr = encode(data)
    if (data.district_id === undefined) {
        if (data.gender != undefined)
            return request({
                url: `/v1/Inves/Teacher/Filter?page=${data.page}&size=5&city_id=${data.city_id}&gender=${data.gender}`,
                method: "post",
                data: { "raw": arr }
            })
        else return request({
            url: `/v1/Inves/Teacher/Filter?page=${data.page}&size=5&city_id=${data.city_id}`,
            method: "post",
            data: { "raw": arr }
        })
    }

    else {
        if (data.gender != undefined)
            return request({
                url: `/v1/Inves/Teacher/Filter?page=${data.page}&size=5&district_id=${data.district_id}&gender=${data.gender}`,
                method: "post",
                data: { "raw": arr }
            })
        else return request({
            url: `/v1/Inves/Teacher/Filter?page=${data.page}&size=5&district_id=${data.district_id}`,
            method: "post",
            data: { "raw": arr }
        })
    }

}

//查询问卷
export const StudentSpecificID = (id: number) => {
    let arr = encode(id)
    return request({
        url: `v1/Inves/Student/SpecificID`,
        method: "post",
        data: { "raw": arr }
    })
}
export const TeacherSpecificID = (id: number) => {
    let arr = encode(id)
    return request({
        url: `v1/Inves/Teacher/SpecificID`,
        method: "post",
        data: { "raw": arr }
    })
}


//获取地区
export const ACRM = (id: number) => {
    return request({
        url: `/v1/ACRM?code=${id}`,
        method: "post"
    })
}


//查看用户
export const filterUser = (page: number, size: number, user_type?: number) => {
    if (user_type !== undefined)
        return request({
            url: `/v1/User/Info/Filter?page=${page}&size=${size}&user_type=${user_type}`,
            method: "post",
        })
    else
        return request({
            url: `/v1/User/Info/Filter?page=${page}&size=${size}`,
            method: "post",
        })

}
//管理员查询用户信息（通过id）
export const UserSpecificID = (data: { id: number }) => {
    let arr = encode(data)

    return request({
        url: `v1/User/Info/SpecificID`,
        method: "post",
        data: { "raw": arr }
    })
}


//管理员更改用户权限
export const AdminUpdateRank = (data: { id: number, rank: string }) => {
    let arr = encode(data)
    return request({
        url: `v1/User/AdminUpdateRank`,
        method: "put",
        data: { "raw": arr }
    })
}
//管理员注销用户
export const AdminUpdateActive = (data: { id: number }, is_active: string) => {
    let arr = encode(data)
    return request({
        url: `v1/User/AdminUpdateActive?is_active=${is_active}`,
        method: "put",
        data: { "raw": arr }
    })
}


//管理员下载问卷
export const Download = (data: { district_id?: number, city_id?: number, type: string, gender: string }) => {
    if (data.type === "student_gz") {
        if (data.district_id !== undefined) {
            if (data.gender != undefined) return request({
                url: `v1/Inves/Download?type=${data.type}&district_id=${data.district_id}&gender=${data.gender}`,
                method: "post",
                responseType: "blob",
                timeout: 30000,
                onDownloadProgress(progress: any) {
                    downLoadNotice(Math.round(100 * progress.loaded / progress.total))

                }
            })
            else return request({
                url: `v1/Inves/Download?type=${data.type}&district_id=${data.district_id}`,
                method: "post",
                responseType: "blob",
                timeout: 30000,
                onDownloadProgress(progress: any) {
                    downLoadNotice(Math.round(100 * progress.loaded / progress.total))

                }
            })
        }

        else {
            if (data.gender != undefined)
                return request({
                    url: `v1/Inves/Download?type=${data.type}&gender=${data.gender}`,
                    method: "post",
                    responseType: "blob",
                    timeout: 30000,
                    onDownloadProgress(progress: any) {
                        downLoadNotice(Math.round(100 * progress.loaded / progress.total))

                    }
                })
            else return request({
                url: `v1/Inves/Download?type=${data.type}`,
                method: "post",
                responseType: "blob",
                timeout: 30000,
                onDownloadProgress(progress: any) {
                    downLoadNotice(Math.round(100 * progress.loaded / progress.total))

                }
            })
        }
    }
    else {
        if (data.district_id !== undefined) {
            if (data.gender != undefined)
                return request({
                    url: `v1/Inves/Download?type=${data.type}&district_id=${data.district_id}&gender=${data.gender}`,
                    method: "post",
                    responseType: "blob",
                    timeout: 30000,
                    onDownloadProgress(progress: any) {
                        downLoadNotice(Math.round(100 * progress.loaded / progress.total))

                    }
                })
            else return request({
                url: `v1/Inves/Download?type=${data.type}&district_id=${data.district_id}`,
                method: "post",
                responseType: "blob",
                timeout: 30000,
                onDownloadProgress(progress: any) {
                    downLoadNotice(Math.round(100 * progress.loaded / progress.total))

                }
            })
        }
        else {
            if (data.gender != undefined)
                return request({
                    url: `v1/Inves/Download?type=${data.type}&city_id=${data.city_id}&gender=${data.gender}`,
                    method: "post",
                    responseType: "blob",
                    timeout: 30000,
                    onDownloadProgress(progress: any) {
                        downLoadNotice(Math.round(100 * progress.loaded / progress.total))

                    }
                })
            else return request({
                url: `v1/Inves/Download?type=${data.type}&city_id=${data.city_id}`,
                method: "post",
                responseType: "blob",
                timeout: 30000,
                onDownloadProgress(progress: any) {
                    downLoadNotice(Math.round(100 * progress.loaded / progress.total))

                }
            })

        }
    }


}


//测评开放状态查询
export const CheckRelease = () => {
    return request({
        url: `v1/Inves/Release/Check/City`,
        method: "post"
    })
}
//创建开放许可
export const ReleaseCreate = (data: { total: number, city_id?: number, begin_time: string, end_time?: string, district_id?: number }) => {
    let arr = encode(data)
    return request({
        url: `v1/Inves/Release/Create`,
        method: "post",
        data: { "raw": arr }
    })
}
export const ReleaseCreateForSuperAdmin = (data: { total: number, city_id?: number, begin_time: string, end_time?: string, district_id?: number }) => {
    let arr = encode(data)
    let url = `v1/Inves/Release/Create`
    if (data.city_id != undefined && data.district_id == undefined) {
        url += `?release_type=city`
    }
    if (data.city_id == undefined && data.district_id != undefined) {
        url += `?release_type=district`
    }
    return request({
        url: url,
        method: "post",
        data: { "raw": arr }
    })
}

//获取开放记录
export const ReleaseInfo = () => {
    return request({
        url: `v1/Inves/Release/Info`,
        method: "post"
    })
}

export const ReleaseInfoForSuperAdmin = (data: { city_id?: number, district_id?: number }) => {
    let url = `v1/Inves/Release/Info`
    if (data.city_id != undefined && data.district_id == undefined) {
        url += `?release_type=city`
    }
    if (data.city_id == undefined && data.district_id != undefined) {
        url += `?release_type=district`
    }
    return request({
        url: url,
        method: "post"
    })
}
//删除开放记录
export const ReleaseDistrict = (district_record: any) => {
    return request({
        url: `v1/Inves/Release?district_record=${district_record}`,
        method: "delete"
    }).then((res: { code: number }) => {
        if (res.code === 611111) {
            ElMessage({
                type: 'success',
                message: "删除成功！"
            })
        }
        return res
    })
}
export const ReleaseDistrictForSuperAdmin = (district_record: any) => {
    return request({
        url: `v1/Inves/Release?district_record=${district_record}&release_type=district`,
        method: "delete"
    }).then((res: { code: number }) => {
        if (res.code === 611111) {
            ElMessage({
                type: 'success',
                message: "删除成功！"
            })
        }
        return res
    })
}
export const ReleaseCity = (city_record: any) => {
    return request({
        url: `v1/Inves/Release?city_record=${city_record}`,
        method: "delete"
    }).then((res: { code: number }) => {
        if (res.code === 611111) {
            ElMessage({
                type: 'success',
                message: "删除成功！"
            })
        }
        return res
    })
}
export const ReleaseCityForSuperAdmin = (city_record: any) => {
    return request({
        url: `v1/Inves/Release?city_record=${city_record}&release_type=city`,
        method: "delete"
    }).then((res: { code: number }) => {
        if (res.code === 611111) {
            ElMessage({
                type: 'success',
                message: "删除成功！"
            })
        }
        return res
    })
}
//用户查询个人信息
export const UserInfo = () => {
    return request({
        url: `v1/User/Info/Self`,
        method: "post"
    })
}
//用户修改用户名
export const UpDateUserName = (data: { username: string }) => {
    let arr = encode(data)
    return request({
        url: `v1/User/Info/Username`,
        method: "put",
        data: { "raw": arr }
    })
}
//用户修改地区
export const UpDateUserArea = (data: { city_id: number, district_id: number }) => {
    return request({
        url: `v1/User/Info/Address?city_id=${data.city_id}&district_id=${data.district_id}`,
        method: "put",
    })
}
//用户查询测评是否开放
export const CheckReleaseDistrict = async () => {
    let flag = false
    await request({
        url: `v1/Inves/Release/Check/District`,
        method: "post"
    }).then((res: any) => {
        if (res.code === 600011) flag = true
    }).catch(() => {

    })
    return flag
}

//管理员将开放许可 置为完成
export const ReleaseDone = (cd_record: string) => {

    return request({
        url: `v1/Inves/Release/Done?district_record=${cd_record}`,
        method: "put",
    })
}
//模糊查询学校已提交的问卷数量
export const InvesSubmitted = (name: string) => {
    return request({
        url: `v1/Inves/Submitted/${name}`,
        method: "post",
    })
}
//用户修改密码
export const UserInfoPwd = (data: { old_password: string, new_password: string }) => {
    let arr = encode(data)
    request({
        url: `v1/User/Info/Pwd`,
        method: "put",
        data: { "raw": arr }
    }).then((res: { code: number }) => {
        if (res.code === 611111) {
            ElMessage({
                type: 'success',
                message: "修改成功！"
            })
        }
    })
}

//用户忘记账号
export const MatchUser = (data: any) => {

    return request({
        url: `v1/User/Match?city_id=${data.city_id}&district_id=${data.district_id}&username=${data.username}`,
        method: "post",
    })
}



